package com.xique.door.project.miniprogram.controller;

import com.xique.common.core.constant.OssFilePrefixConstants;
import com.xique.common.core.domain.R;
import com.xique.common.core.enums.StatusCode;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.door.bean.domain.Repair;
import com.xique.door.project.miniprogram.service.IMiniRepairService;
import com.xique.system.api.RemoteSystemDictService;
import com.xique.system.api.dto.SystemDictDataDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 报修
 */
@RestController
@RequestMapping("/mine/repair")
public class MiniRepairController {

    @Autowired
    private IMiniRepairService miniRepairService;

    @Autowired
    private AliossUtil aliossUtil;

    @Autowired
    private RemoteSystemDictService remoteSystemDictService;

    @ResponseBody
    @PostMapping("/uploadMineImage")
    @Log(title = "oss维修图片", businessType = BusinessType.IMPORT)
    @ApiOperation("oss维修图片")
    public AjaxResult uploadMineImage(@RequestParam("file") MultipartFile file) {
        if (file != null) {
            Map<String, Object> resultMap =
                    aliossUtil.uploadFile(file, OssFilePrefixConstants.MINI_IMGAGE);
            if (resultMap.get("code").equals(StatusCode.ERROR.getCode())) {
                return AjaxResult.error("error", "文件上传失败！！！");
            }
            return AjaxResult.success("success", resultMap.get("url"));
        } else {
            return AjaxResult.error("error", "文件上传失败！！！");
        }
    }

    /**
     * 添加报修
     */
    @PostMapping("/addRepair")
    public AjaxResult insertMiniRepair(@RequestBody Repair repair, HttpServletRequest request) {

        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            repair.setBaseUserId(baseUserId);
            return miniRepairService.insertMiniRepair(repair);
        } else {
            return AjaxResult.success("请登录");
        }
    }

    /**
     * 报修详情
     */
    @GetMapping("/repairDetails")
    public AjaxResult getRepair(Long id, HttpServletRequest request) {
        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            return miniRepairService.getMiniRepair(id, null);
        } else {
            return AjaxResult.success("请登录");
        }
    }

    /**
     * 报修列表
     */
    @GetMapping("/repairList")
    public AjaxResult getRepairList(
            Integer pageNum, Integer pageSize, String status,Long parkId, HttpServletRequest request) {
        pageNum = pageNum != null ? pageNum : 1;
        pageSize = pageSize != null ? pageSize : 10;
        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            return AjaxResult.success(
                    miniRepairService.getRepairList(status, pageNum, pageSize, parkId, null));
        }else {
            return AjaxResult.success("请登录");
        }
    }

    @GetMapping("/repairTypeList/{dictType}")
    public AjaxResult getRepairTypeList(@PathVariable String dictType) {
        R<List<SystemDictDataDTO>> result = remoteSystemDictService.getDictDataByType(dictType);
        if (R.FAIL == result.getCode()) {
            return AjaxResult.success(new ArrayList<>());
        }
        return AjaxResult.success(result.getData());
    }

    @GetMapping("/parkInfo")
    public AjaxResult seleceteParkInfo(){
        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            return miniRepairService.seleceteParkInfo();
        }else {
            return AjaxResult.success("请登录");
        }
    }
}
